from wormbane.ui.selection_dialog import selection_dialog

def command_drop(game, ui):
    if not game.hero.inv:
        game.write(game.hero, 'has nothing to drop')
        return
    droppable = [item for item in game.hero.inv
                 if not (item.is_armor and item.worn)]
    if not droppable:
        game.write(game.hero, 'cannot drop anything')
        return
    droppable.reverse()

    if len(droppable) == 1 and droppable[0].count == 1:
        selection = {droppable[0]: 1}
    else:
        selection = selection_dialog(ui.stdscr, droppable, prompt = 'Drop')
        if not selection:
            game.write('Never mind.')
            return

    for item, count in selection.iteritems():
        item = item.split(count)
        game.write(game.hero, 'drops', item)
        item.move_to(game.hero.env)
        if game.hero.env.feature.is_altar:
            if item.blessed:
                game.write(item, 'flashes white')
            elif item.cursed:
                game.write(item, 'flashes black')
            item.beatitude_id = True
        item.merge()
    game.hero_moved = True
